package com.wsz.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsz.system.pojo.Menu;
import com.wsz.system.pojo.Role;
import com.wsz.system.pojo.RoleMenu;
import com.wsz.system.pojo.UserRole;
import com.wsz.system.service.MenuService;
import com.wsz.system.mapper.MenuMapper;
import com.wsz.system.service.RoleMenuService;
import com.wsz.system.service.UserRoleService;
import com.wsz.system.utils.RouterHelper;
import com.wsz.vo.AssginMenuVo;
import com.wsz.vo.RouterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author wangs
* @description 针对表【sys_menu(菜单表)】的数据库操作Service实现
* @createDate 2022-11-09 16:47:42
*/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu>
    implements MenuService{

    @Autowired
    private RoleMenuService roleMenuService;

    @Autowired
    private UserRoleService userRoleService;

    @Override
    public List<Menu> findsNodes() {
        List<Menu> menus = baseMapper.selectList(null);

        List<Menu> resultList = bulidTree(menus);
        return resultList;
    }

    @Override
    public boolean removeMenuById(String id) {

        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        Integer integer = baseMapper.selectCount(queryWrapper);

        if(integer > 1) return false;

        int i = baseMapper.deleteById(id);
        System.out.println(i);

        if(i > 0) return true;
        return false;
    }

    @Override
    public List<Menu> findMenuByRoleId(String roleId) {

        //根据角色Id查询角色分配过的菜单列表
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        queryWrapper.select("menu_id");
        List<RoleMenu> list = roleMenuService.list(queryWrapper);

        List<String> menuList = new ArrayList<>();

        for(RoleMenu rm : list) menuList.add(rm.getMenuId());

        QueryWrapper<Menu> queryWrapper1 = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        List<Menu> menuList1 = baseMapper.selectList(queryWrapper1);

        for(Menu menu : menuList1){
            if(menuList.contains(menu.getId())){
                menu.setSelect(true);
            }else{
                menu.setSelect(false);
            }
        }

        List<Menu> menuList2 = bulidTree(menuList1);

        return menuList2;
    }

    //给角色分配菜单权限
    @Override
    public void doAssign(AssginMenuVo assginMenuVo) {
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",assginMenuVo.getRoleId());
        roleMenuService.remove(queryWrapper);

        List<String> menuIdList = assginMenuVo.getMenuIdList();

        for(String mid : menuIdList){
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setMenuId(mid);
            roleMenu.setRoleId(assginMenuVo.getRoleId());

            roleMenuService.save(roleMenu);
        }
    }

    //查询userId的菜单
    @Override
    public List<RouterVo> getUserMenuList(String userId) {

        List<Menu> menuList = null;

        if("1".equals(userId)){
            QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status",1);
            queryWrapper.orderByAsc("id");
             menuList = baseMapper.selectList(queryWrapper);
             menuList = this.bulidTree(menuList);
        }else {
            menuList = this.findMenuByUserId(userId);
        }

        List<RouterVo> menuTreeList = RouterHelper.buildRouters(menuList);
        return menuTreeList;
    }

    //根据用户Id查询菜单
    private List<Menu> findMenuByUserId(String userId) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).select("role_id");
        List<UserRole> list = userRoleService.list(queryWrapper);

        List<String> roleIds = new ArrayList<>();

        for(UserRole userRole : list){
            String roleId = userRole.getRoleId();
            roleIds.add(roleId);
        }

        QueryWrapper<RoleMenu> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in("role_id",roleIds).select("menu_id");
        List<RoleMenu> list1 = roleMenuService.list(queryWrapper1);

        List<String> menuIds = new ArrayList<>();

        for(RoleMenu roleMenu : list1){
            menuIds.add(roleMenu.getMenuId());
        }

        QueryWrapper<Menu> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.in("id",menuIds).eq("status",1);
        List<Menu> menuList = baseMapper.selectList(queryWrapper2);

        List<Menu> menuList2 = this.bulidTree(menuList);

        return menuList2;
    }


    //查询userId的按钮
    @Override
    public List<String> getUserButtonList(String id) {
        List<Menu> menuList = null;

        if("1".equals(id)){
            menuList = baseMapper.selectList(new QueryWrapper<Menu>().eq("status",1));
        }else{
            menuList = this.findMenuByRoleId(id);
        }

        List<String> permiss = new ArrayList<>();
        for(Menu menu : menuList){
            if(menu.getType() == 2)
            permiss.add(menu.getPerms());
        }
        return permiss;
    }

    //构建树形结构
    private  List<Menu> bulidTree(List<Menu> menus) {
        List<Menu> list = new ArrayList<>();
        for(Menu menuFather : menus){
            if(menuFather.getParentId().equals("0")){
                list.add(findSon(menuFather,menus));
            }
        }
        return list;
    }

    //找儿子
    private  Menu findSon(Menu menuFather, List<Menu> menus) {
        menuFather.setChildren(new ArrayList<Menu>());

        for(Menu menuSon : menus){
            String parentId = menuSon.getParentId();
            String id = menuFather.getId();

            if(id.equals(parentId)){
                if(menuFather.getChildren()==null) {
                    menuFather.setChildren(new ArrayList<>());
                }
                menuFather.getChildren().add(findSon(menuSon,menus));
            }
        }
        return menuFather;
    }


}




